//
//  MoreView.swift
//  YueChat
//
//  Created by IMac on 2024/9/23.
//

import UIKit

class MoreView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var data = [String]()
    var imgs = [String]()
    var moreSendBlock: (_ index:Int) -> Void = {index in }
    var type = Int()

    lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kScreenWidth/5, height: 100)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        return layout
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height:  300), collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.backgroundColor = kGrayBG
        collectionView.delegate = self
        self.addSubview(collectionView)
        return collectionView
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRect(x: 0, y: kScreenHeight - 300, width: kScreenWidth, height: 300)
        self.loadData()
        self.backgroundColor = kGrayBG
        self.addSubview(collectionView)
        collectionView.register(UINib(nibName: "MoreViewCell", bundle: nil), forCellWithReuseIdentifier: "MoreViewCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setType (type:Int) {
        self.type = type

        if type == 0 {
            self.data = [
                KLocalizedString("ConImage"),
                KLocalizedString("ConVedio"),
                KLocalizedString("ConCard"),
                KLocalizedString("ConFile")
//                KLocalizedString("videoCall"),
//                KLocalizedString("audioCall")
            ]
            self.imgs = [
                "c_phone",
                "c_video",
                "c_card",
                "c_file",
                "c_sendbill", // 注意这里有七个元素，但只用了六个
//                "c_vedioCall",
//                "c_audioCall"
            ]
        } else {
            self.data = [
                KLocalizedString("ConImage"),
                KLocalizedString("ConVedio"),
                KLocalizedString("ConCard"),
                KLocalizedString("ConFile")
            ]
            self.imgs = [
                "c_phone",
                "c_video",
                "c_card",
                "c_file"
            ]
        }

//        let login = MHHYInfoManager.shared.currentInfo
        
//        if type == 0 {
//            self.frame = CGRect(x: 0, y: kScreenHeight - 300, width: kScreenWidth, height: 300)
//        } else {
//            self.frame = CGRect(x: 0, y: kScreenHeight - 150, width: kScreenWidth, height: 150)
//        }

        collectionView.reloadData()
    }
    
    
    func loadData() {
       
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MoreViewCell", for: indexPath) as! MoreViewCell
        let info = data[indexPath.row]
        cell.nameLab.text = info
        cell.img.image = UIImage(named: imgs[indexPath.row])
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        moreSendBlock(indexPath.row)
    }
}
